package com.easydl.project.service;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.easydl.project.model.EasyDatasetListRestltVO;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * https://ai.baidu.com/ai-doc/EASYDL/0k38n32n7
 * @author fxb208
 *
 */
@Service
public class EasydlDatasetService {
	
	private Logger logger=LoggerFactory.getLogger(this.getClass());
	
	//数据集创建API
	@Value("${easydl.dataset.url.create}")
	private String datasetCreateUrl;
	
	//数据集列表API
	@Value("${easydl.dataset.url.list}")
	private String datasetListUrl;
	
	//数据集删除API
	@Value("${easydl.dataset.url.delete}")
	private String datasetdeleteUrl;
	
	//添加数据API
	@Value("${easydl.dataset.url.addentity}")
	private String datasetAddentityUrl;
	
	//分类（标签）列表API
	@Value("${easydl.label.list.url}")
	private String labelListUrl;
	
	//分类（标签）删除API
	@Value("${easydl.label.delete.url}")
	private String labelDeleteUrl;
	
	@Qualifier(value="easyRestTemplate")
	@Autowired
	private RestTemplate easyRestTemplate;
	
	@Autowired
	private EasyLockTokenService easyTokenService;
	
	public EasyDatasetListRestltVO queryDatasetList() {
		String accessToken =easyTokenService.getToken().getAccess_token();
		Map<String, Object> map = new HashMap<>();
		map.put("type", "OBJECT_DETECTION");
		map.put("start", 0);
		map.put("num", 20);
		ObjectMapper objectMapper=new ObjectMapper();
		String param=null;
		try {
			param = objectMapper.writeValueAsString(map);
		} catch (JsonProcessingException e) {
			logger.error(e.getMessage(), e);
			return null;
		}
		EasyDatasetListRestltVO easyDatasetListRestltVO=
				easyRestTemplate.postForObject(
						this.datasetListUrl+"?access_token=" + accessToken,
						param, EasyDatasetListRestltVO.class);
		return easyDatasetListRestltVO;
	}
	
	
	
}
